package cs342.uwosh.edu.ContactsApp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import cs342.uwosh.edu.ContactsApp.R;

public class LoginPage extends Activity {
	
	private String password = "passw0rd";
	private String title = "Enter Password";
	private String message = "Please Enter Password";
	private ContactsDataSource db;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		db = new ContactsDataSource(this);
		alert();
	}
	
	public void alert() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle(title);
        alert.setMessage(message);

        // Set an EditText view to get user input 
		final EditText input = new EditText(this);
		input.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
		input.setTransformationMethod(PasswordTransformationMethod.getInstance());
		
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				
				if(input.getText().toString().compareTo(password)==0) {
					update();
					Intent i = new Intent(LoginPage.this, ContactsActivity.class);
		    		startActivity(i);
				}else {
					message = "Incorrect Password!";
					alert();
				}
				
			}
		});
		
		alert.show();
	}
	
	public boolean update() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://localhost:3306/contacts";
			String user = "root";
			String myPassword = "1234";
			conn = DriverManager.getConnection(url, user, myPassword);
			Statement statement = conn.createStatement();
			ResultSet results = statement.executeQuery("SELECT * FROM contacts");
			
			ArrayList<Contact> contacts = new ArrayList<Contact>();
			
			boolean moreResults=true;
			
			while(moreResults) {				
				String name = results.getString(ContactsDatabaseHelper.COL_NAME);
				String hPhone = results.getString(ContactsDatabaseHelper.COL_HOME_PHONE_NUMBER);
				String cPhone = results.getString(ContactsDatabaseHelper.COL_CELL_PHONE_NUMBER);
				String aPhone = results.getString(ContactsDatabaseHelper.COL_ALTERNATE_PHONE_NUMBER);
				String email = results.getString(ContactsDatabaseHelper.COL_EMAIL);
				String address = results.getString(ContactsDatabaseHelper.COL_ADDRESS);
				String comment = results.getString(ContactsDatabaseHelper.COL_COMMENT);
				
				if(name==null) {
					name = "";
				}
				if(hPhone==null) {
					hPhone = "";
				}
				if(cPhone==null) {
					cPhone = "";
				}
				if(aPhone==null) {
					aPhone = "";
				}
				if(email==null) {
					email = "";
				}
				if(address==null) {
					address = "";
				}
				if(comment==null) {
					comment = "";
				}
				Contact contact = new Contact(0,name,address,hPhone,cPhone,aPhone,email,comment); 
				contacts.add(contact);
				moreResults = results.next();
			}
			conn.close();
			db.open();
			for(int i = 0; i < contacts.size(); i++) {
				db.createContact(contacts.get(i));
			}
			db.close();
			
		} catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
